package com.example.yunifang.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yunifang.ClassSeachActivity;
import com.example.yunifang.FenLeiActivity;
import com.example.yunifang.QuanBuActivity;
import com.example.yunifang.R;
import com.example.yunifang.XiangQingActivity;
import com.example.yunifang.adapter.Fragment2GridViewAdapter;
import com.example.yunifang.bean.FeiLeiBean;
import com.example.yunifang.bean.FeiLeiBean.Data.Category;
import com.example.yunifang.bean.FeiLeiBean.Data.GoodsBrief;
import com.example.yunifang.myview.MyZhuGridView;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class Fragment2 extends Fragment implements OnClickListener {
	private Context context;
	private RelativeLayout layout;
	private LinearLayout fragment_text_tv1;
	private ImageView fragment2_fenlei_gongxiao;
	private FeiLeiBean bean;
	private ImageView fragment2_fenlei_shuxing;
	private ImageView fragment2_fenlei_fuzhi;
	private Button fragment2_fenlei_button1;
	private Button fragment2_fenlei_button2;
	private Button fragment2_fenlei_button3;
	private Button fragment2_fenlei_button4;
	private Button fragment2_fenlei_button5;
	private Button fragment2_fenlei_button6;
	private MyZhuGridView fragment2_fenlei_gv;
	ArrayList<GoodsBrief> data=new ArrayList<GoodsBrief>();
	private TextView bt_more;
	private RadioButton class_serach_ed;
	private TextView serach_tt;
	public static int y=0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = View.inflate(getActivity(), R.layout.fragment2, null);
		init(view);

		return view;
	}

	public void init(View view) {
		layout = (RelativeLayout) view.findViewById(R.id.include_no_wifi2);
		fragment_text_tv1 = (LinearLayout) view.findViewById(R.id.tv_2_2);
		context = getActivity();
		RadioButton button = (RadioButton) view.findViewById(R.id.no_wifi_btn);
		getWe();
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getWe();
			}
		});
		fragment2_fenlei_gongxiao = (ImageView) view
				.findViewById(R.id.fragment2_fenlei_gongxiao);
		fragment2_fenlei_shuxing = (ImageView) view
				.findViewById(R.id.fragment2_fenlei_shuxing);
		fragment2_fenlei_fuzhi = (ImageView) view
				.findViewById(R.id.fragment2_fenlei_fuzhi);
		fragment2_fenlei_button1=(Button)view.findViewById(R.id.fragment2_fenlei_button1);
		fragment2_fenlei_button2=(Button)view.findViewById(R.id.fragment2_fenlei_button2);
		fragment2_fenlei_button3=(Button)view.findViewById(R.id.fragment2_fenlei_button3);
		fragment2_fenlei_button4=(Button)view.findViewById(R.id.fragment2_fenlei_button4);
		fragment2_fenlei_button5=(Button)view.findViewById(R.id.fragment2_fenlei_button5);
		fragment2_fenlei_button6=(Button)view.findViewById(R.id.fragment2_fenlei_button6);
		fragment2_fenlei_gv=(MyZhuGridView)view.findViewById(R.id.fragment2_fenlei_gv);
		bt_more=(TextView)view.findViewById(R.id.bt_more);
		class_serach_ed = (RadioButton) view.findViewById(R.id.class_serach_ed);
		serach_tt = (TextView) view.findViewById(R.id.serach_bt);
		SerachJump();
	}

	private void getJson() {
		// TODO Auto-generated method stub
		HttpUtils utils = new HttpUtils();
		utils.send(
				HttpMethod.GET,
				"http://m.yunifang.com/yunifang/mobile/category/list?random=9503&encode=e181334d0cd93bf2ec95f3c9f85d1dde",
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						SharedPreferences sp = getActivity()
								.getSharedPreferences("fragment",
										getActivity().MODE_PRIVATE);
						String json = sp.getString("fragment2_1", null);
						if(json!=null){
							Gson gson = new Gson();
							bean = gson.fromJson(json, FeiLeiBean.class);
							initData(bean);
						}
					}
					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						Gson gson = new Gson();
						String json = arg0.result;
						SharedPreferences sp = getActivity().getSharedPreferences("fragment1",
								getActivity().MODE_PRIVATE);
						Editor editor = sp.edit();
						editor.putString("fragment2_1", json);
						editor.commit();
						bean = gson.fromJson(json, FeiLeiBean.class);
						initData(bean);
					}
				});
	}

	protected void initData(final FeiLeiBean bean) {
		// TODO Auto-generated method stub
		fragment2_fenlei_gongxiao.setOnClickListener(this);
		fragment2_fenlei_shuxing.setOnClickListener(this);
		fragment2_fenlei_fuzhi.setOnClickListener(this);
		fragment2_fenlei_button1.setOnClickListener(this);
		fragment2_fenlei_button2.setOnClickListener(this);
		fragment2_fenlei_button3.setOnClickListener(this);
		fragment2_fenlei_button4.setOnClickListener(this);
		fragment2_fenlei_button5.setOnClickListener(this);
		fragment2_fenlei_button6.setOnClickListener(this);
		bt_more.setOnClickListener(this);
		data.clear();
		data.addAll(bean.data.goodsBrief);
		Fragment2GridViewAdapter adapter=new Fragment2GridViewAdapter(data, context);
		fragment2_fenlei_gv.setAdapter(adapter);
		fragment2_fenlei_gv.setFocusable(false);
		fragment2_fenlei_gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				y=1;
				String id=bean.data.goodsBrief.get(arg2).id;
				int ids=Integer.parseInt(id);
				Intent intent=new Intent(getActivity(),XiangQingActivity.class);
				intent.putExtra("id", ids);
				startActivity(intent);
				((Activity) context).overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			}
		});
		//fragment2_scroll.setFocusable(false);
		//fragment2_fenlei_gv.setFocusable(true);
	}

	public void getWe() {
		// TODO Auto-generated method stub
		if (isNetworkAvailable(context)) {
			Toast.makeText(context.getApplicationContext(), "当前网络可用",
					0).show();
			fragment_text_tv1.setVisibility(View.VISIBLE);
			layout.setVisibility(View.GONE);
			getJson();
		} else {
			Toast.makeText(context.getApplicationContext(), "当前网络不可用",
					0).show();
			fragment_text_tv1.setVisibility(View.GONE);
			layout.setVisibility(View.VISIBLE);
		}
	}

	private boolean isNetworkAvailable(Context zhuActivity) {
		// TODO Auto-generated method stub
		Context context = zhuActivity.getApplicationContext();
		// ��ȡ�ֻ��������ӹ�����󣨰�����wi-fi,net�����ӵĹ��?
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivityManager == null) {
			return false;
		} else {
			// ��ȡNetworkInfo����
			NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

			if (networkInfo != null && networkInfo.length > 0) {
				for (int i = 0; i < networkInfo.length; i++) {
					// System.out.println(i + "===״̬===" +
					// networkInfo[i].getState());
					// System.out.println(i + "===����===" +
					// networkInfo[i].getTypeName());
					// �жϵ�ǰ����״̬�Ƿ�Ϊ����״̬
					if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.fragment2_fenlei_gongxiao:
			Intent intent = new Intent(getActivity(), FenLeiActivity.class);
			Category name=bean.data.category.get(0);
			intent.putExtra("category", name);
			startActivity(intent);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_shuxing:
			Intent intent2 = new Intent(getActivity(), FenLeiActivity.class);
			Category name2=bean.data.category.get(1);
			intent2.putExtra("category", name2);
			startActivity(intent2);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_fuzhi:
			Intent intent3 = new Intent(getActivity(), FenLeiActivity.class);
			Category name3=bean.data.category.get(2);
			intent3.putExtra("category", name3);
			startActivity(intent3);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_button1:
			Intent intent4=new Intent(getActivity(),FenLeiActivity.class);
			int fragment_id=0;
			Category name4=bean.data.category.get(0);
			intent4.putExtra("fragment_id", fragment_id);
			intent4.putExtra("category", name4);
			startActivity(intent4);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_button2:
			Intent intent5=new Intent(getActivity(),FenLeiActivity.class);
			int fragment_id5=1;
			Category name5=bean.data.category.get(0);
			intent5.putExtra("fragment_id", fragment_id5);
			intent5.putExtra("category", name5);
			startActivity(intent5);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_button3:
			Intent intent6=new Intent(getActivity(),FenLeiActivity.class);
			int fragment_id6=4;
			Category name6=bean.data.category.get(1);
			intent6.putExtra("fragment_id", fragment_id6);
			intent6.putExtra("category", name6);
			startActivity(intent6);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_button4:
			Intent intent7=new Intent(getActivity(),FenLeiActivity.class);
			int fragment_id7=0;
			Category name7=bean.data.category.get(1);
			intent7.putExtra("fragment_id", fragment_id7);
			intent7.putExtra("category", name7);
			startActivity(intent7);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_button5:
			Intent intent8=new Intent(getActivity(),FenLeiActivity.class);
			int fragment_id8=2;
			Category name8=bean.data.category.get(0);
			intent8.putExtra("fragment_id", fragment_id8);
			intent8.putExtra("category", name8);
			startActivity(intent8);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.fragment2_fenlei_button6:
			Intent intent9=new Intent(getActivity(),FenLeiActivity.class);
			int fragment_id9=3;
			Category name9=bean.data.category.get(1);
			intent9.putExtra("fragment_id", fragment_id9);
			intent9.putExtra("category", name9);
			startActivity(intent9);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		case R.id.bt_more:
			Intent intent10=new Intent(getActivity(),QuanBuActivity.class);
			startActivity(intent10);
			getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
			break;
		default:
			break;
		}
	}
	private void SerachJump() {
		// 强制关闭输入框软键盘
		// class_serach_ed.setInputType(InputType.TYPE_NULL);
		//
		// class_serach_ed.setFocusable(false);
		// class_serach_ed.setOnFocusChangeListener(new OnFocusChangeListener()
		// {
		//
		// @Override
		// public void onFocusChange(View v, boolean hasFocus) {
		// // TODO Auto-generated method stub
		// if(hasFocus)
		// { Toast.makeText(getActivity(), hasFocus+"", 0).show();
		//
		// }
		//
		// }
		// });
		// 点击图像进行跳转JUMP
		serach_tt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(getActivity(),
						ClassSeachActivity.class);
				startActivity(intent);
				((Activity) context).overridePendingTransition(
						R.anim.outactivity, R.anim.inactivity);

			}
		});
		// 点击搜索按钮
		class_serach_ed
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub

						if (isChecked) {
							class_serach_ed.setChecked(false);
							Intent intent = new Intent(getActivity(),
									ClassSeachActivity.class);
							startActivity(intent);
							((Activity) context).overridePendingTransition(
									R.anim.outactivity, R.anim.inactivity);
						}
					}
				});
	}


}
